<?php
/**
 * Created by PhpStorm.
 * User: "lijianguo"
 * Date: 2018/5/15
 * Time: 14:19
 */
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Config;
use Illuminate\Http\Request;

class ConfigController extends Controller
{
    public function __construct()
    {
        view()->share('menu','config');
    }

    /**
     * 条例列表
     * @author:
     * @date: 2018/5/15
     * @return
     */
    public function index()
    {
        $model = new Config();
        $list =$model->orderBy('id','desc')->paginate(10);
        foreach ($list as $key=>$item){
            $list[$key]['value'] = mb_substr($item['value'],0,30); //显示前30个字符
        }
        $page_num = $list->firstItem();
        return view('admin/config/index',compact('list','page_num'));
    }

    /**
     * 添加条例
     * @author:
     * @date: 2018/5/15
     * @return
     */
    public function create()
    {
        return view('admin/config/create');
    }

    /**
     * 把添加条例写入数据库
     * @author:
     * @date: 2018/5/15
     * @return
     */
    public function store(Request $request)
    {
        $key = $request->get('key');
        $value = $request->get('value');
        if (empty($key)){
            return back()->with(['msg'=>'条例类型不能为空','status'=>0]);
        }
        if (empty($value)){
            return back()->with(['msg'=>'条例内容不能为空','status'=>0]);
        }
        $model = new Config();
        $model->create(['key'=>$key,'value'=>$value]);
        return back()->with(['msg' => '添加成功', 'status' => 1, 'msg_url' => '/admin/config']);

        exit();
    }

    /**
     * 编辑条例
     * @author:
     * @date: 2018/5/15
     * @param Request $request
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request,$id)
    {
        $list = Config::find($id);
        return view('admin/config/edit',compact('list'));
    }

    /**
     * 把编辑条例写入数据库
     * @author:
     * @date: 2018/5/15
     * @return
     */

    public function update(Request $request, $id)
    {
        $my_type = $request->get('my_type');
        if($my_type == 2) {
            $value = $request->get('value');
        }else{
            $value = $request->get('value1');
        }
        $key = $request->get('key');

        if (empty($key)){
            return back()->with(['msg'=>'条例类型不能为空','status'=>0]);
        }
        if (empty($value)){
            return back()->with(['msg'=>'条例内容不能为空','status'=>0]);
        }

        Config::where(['id'=>$id])->update(['key'=>$key,'value'=>$value]);
        return back()->with(['msg' => '更新成功', 'status' => 1, 'msg_url' => '/admin/config']);
    }

    /**
     * 删除条例
     * @author:
     * @date: 2018/5/15
     * @return
     */
    public function delete($id)
    {
        $res = Config::destroy($id);
        if($res) {
            $msg = ['static'=>1,'msg'=>'删除成功'];
        } else {
            $msg = ['static'=>0,'msg'=>'删除失败'];
        }
        echo json_encode($msg);exit;
    }

}